package com.lee.mysql.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lee.mysql.dao.TableConfigDao;
import com.lee.mysql.dao.TableDbInfoDao;
import com.lee.mysql.pojo.TableConfig;
import com.lee.mysql.pojo.User;
import com.lee.mysql.util.IdGenUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * 表同步配置
 */
@Slf4j
@Service
@Transactional
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
public class TableConfigService {
    @Autowired
    private TableConfigDao tableConfigDao;
    @Autowired
    private TableDbInfoDao tableDbInfoDao;



    /** 新增表同步配置 */
    public int addRecord(TableConfig record){
        record.setId(IdGenUtil.flakeId());
        if (record.getIsPublic() != null && "0".equals(record.getIsPublic())){
            User user = (User) SecurityUtils.getSubject().getPrincipal();
            record.setUserId(user.getId());
        }

        if (StringUtils.isBlank(record.getConType())){
            record.setConType("1");
        }

        return tableConfigDao.insertOne(record);
    }

    /** 根据ID修改记录 */
    public int updateOneById(TableConfig record){
        if (record.getIsPublic() != null && "0".equals(record.getIsPublic())){
            User user = (User) SecurityUtils.getSubject().getPrincipal();
            record.setUserId(user.getId());
        }

        if (StringUtils.isBlank(record.getConType())){
            record.setConType("1");
        }

        return tableConfigDao.updateById(record);
    }

    /** 根据ID批量删除记录 */
    public int deleteByIds(List<String> idList){
        int i = tableConfigDao.deleteByIds(idList);
        if (i > 0){
            tableDbInfoDao.deleteByConfigIds(idList);
        }

        return i;
    }

    /** 根据ID查询记录 */
    public TableConfig queryOneById(String id){
        return tableConfigDao.queryOneById(id);
    }


    /** 查询分页数据 */
    public PageInfo<TableConfig> queryPaging(TableConfig record, Integer page, Integer size){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        record.setUserId(user.getId());

        PageHelper.startPage(page, size);
        List<TableConfig> list = tableConfigDao.getList(record);
        if (list.size() > 0){
            list.forEach(con->{
                if (StringUtils.isBlank(con.getUserId())){
                    con.setIsPublic("1");
                }else {
                    con.setIsPublic("0");
                }
            });
        }

        return new PageInfo<>(list);
    }


    /** 获取所有启用配置 */
    public List<TableConfig> queryAllConfig(String conType){
        User user = (User) SecurityUtils.getSubject().getPrincipal();

        return tableConfigDao.queryAllConfig(user.getId(),conType);
    }


}
